package org.aspectsense.examples.adaptivemail;
import java.lang.reflect.Field;
import java.util.logging.Logger;

import org.aspectsense.middleware.annotations.AspectSense;
import org.aspectsense.middleware.annotations.RequiredService;
import org.aspectsense.middleware.annotations.Service;
import org.aspectsense.middleware.interfaces.MailUIService;
import org.aspectsense.middleware.interfaces.MailingService;


@AspectSense()
@Service(
		displayID = "Mail Service", 
		serviceType = org.aspectsense.examples.adaptivemail.MailService.class,
		utilityFunction = "context (#bandwidth.cost) * ((utility (MailingService) + utility (MailUIService))/100)",
		isEndUserService = true
		)
public class MailServiceImpl implements MailService{
	
	private static final Logger logger = Logger.getLogger(MailServiceImpl.class.getCanonicalName());

	//annotate the fields that represent required services
	@RequiredService(displayID= "mailUIService")
	private MailUIService mailUIService=null;
	@RequiredService(displayID= "mailingService")
	private MailingService mailingService=null;
	
	//private String[] mailInfo, attachments=null;
	//private String mailServer, from, to, subject, messageBody=null;
	
	
	public void set (MailUIService ms)
	{
		mailUIService = ms;
	}
	public void unset (MailUIService ms)
	{
		
	}
	
	public MailServiceImpl(){
		
	}
	
	public void activate(){
		/*
		mailInfo = mailUIService.getMailInfo();
		mailServer = mailInfo[0];
		from = mailInfo[1];
		to = mailInfo[2];
		subject = mailInfo[3];
		messageBody = mailInfo[4];
		attachments = null;
		try {
			mailingService.sendMail(mailServer, from, to, subject, messageBody, attachments);
		} catch (AddressException e) {
			logger.info("MailServiceImpl::Error while sending Email!(Invalid email Address)");
		} catch (MessagingException e) {
			logger.info("MailServiceImpl::Error while sending Email!(MessagingException)");
			e.printStackTrace();
		}*/
		
	}
	public void deactivate(){
		//todo
	}

	//This code is how the adaptation manager will decide how to connect the components
	public static void main(String [] args)
	{
		/*MailServiceImpl mailServiceImpl = null; // todo
		Field field = MailServiceImpl.class.getDeclaredField("mailUIService");
		MailUIService mailUIService = null; // todo
		field.set(mailServiceImpl, mailUIService);*/
	}
}
